package pl.gpms.server.persistance.map;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import pl.gpms.client.dto.EstimateDTO;
import pl.gpms.client.dto.ObjectsDTO;

@Entity
@Table(name = "T_OBJECT")
public class Objects implements Serializable {

	private static final long serialVersionUID = 1L;
	private Long id;
	private String name;
	private String name2;
	private String attrib;
	private Date date;
	private List<Estimate> objectEstimate;
	
	public Objects() {};
	public Objects( String name, String name2, String attrib, Date date ) {
		this.name = name;
		this.name2 = name2;
		this.attrib = attrib;
		this.date = date;
	}
	public Objects( ObjectsDTO objects ) {
		SimpleDateFormat formatter = new SimpleDateFormat("d/M/yyyy");
		
		this.id = objects.getId();
		this.name = objects.getName();
		this.name2 = objects.getName2();
		this.attrib = objects.getAttrib();
		if(objects.getDateForClient() != null)
			this.date = objects.getDateForClient();
		else {
			try {
				this.date = formatter.parse(objects.getDate());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if ( objects.getObjectEstimate() != null ) {
			List<Estimate> tmpEstimate = new ArrayList<Estimate>(objects.getObjectEstimate().size());
			for(EstimateDTO estimateDTO : objects.getObjectEstimate())
				tmpEstimate.add(new Estimate(estimateDTO));
		}
	}
	
	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column( name="OBJ_ID", unique = true, nullable = false )
	public Long getId() {
		return this.id;
	}
	
	public void setId( Long id ) {
		this.id = id;
	}
	
	@Column( name="OBJ_NAME", unique = false, nullable = false, length = 45 )
	public String getName() {
		return name;
	}
	
	public void setName( String name ) {
		this.name = name;
	}
	
	@Column( name="OBJ_NAME_2", unique = false, nullable = false, length = 45 )
	public String getName2() {
		return name2;
	}
	
	public void setName2( String name2 ) {
		this.name2 = name2;
	}
	
	@Column( name="OBJ_ATTRIB", unique = false, nullable = false, length = 20 )
	public String getAttrib() {
		return attrib;
	}
	
	public void setAttrib( String attrib ) {
		this.attrib = attrib;
	}
	
	@Column( name="OBJ_DATE", unique = false, nullable = false )
	public Date getDate() {
		return date;
	}
	
	public void setDate( Date date ) {
		this.date = date;
	}
	
	@OneToMany( mappedBy = "estimateObject" )
	public List<Estimate> getObjectEstimate() {
		return objectEstimate;
	}
	
	public void setObjectEstimate( List<Estimate> objectEstimate ) {
		this.objectEstimate = objectEstimate;
	}
	
	@Transient
	public int getObjectDate() {
		Calendar dateOfObject = new GregorianCalendar();
		dateOfObject.setTime(date);
		Calendar today = new GregorianCalendar();
		int dateResult = today.get(Calendar.YEAR) - dateOfObject.get(Calendar.YEAR);
		return dateResult;
	}
}
